<?php
/**
 * 修改擅长标签
 * @gaojian3
 * 2012年2月23日, PM 10:01:25
 */

include('../../__global.php');
include(SERVER_ROOT_PATH.'/include/config/dataRule/ml_datarule_user.php');
class aj_updateMyTag extends ml_controller
{
    private $uid;
    private $tags;
    private $obrief;
    private $oldTagArr;
    private $tagArr;

    public function initParam()
    {
        $this->uid = $this->__visitor['uid'];
        $this->tags = $this->input('tags','P');            //接收新tag字符串
        $this->tagArr=explode(',',$this->tags);
        $this->obrief=new ml_model_dbUserBriefInfo();
        $this->obrief->getSkillTags($this->uid);
        $this->oldTagArr = $this->obrief->get_data();
        $this->oldTagArr = $this->oldTagArr ['skill_tags'];
        
    }

    public function checkParam()
    {
        $this->check_referer();
        if(!$this->check_permission(ML_PERMISSION_LOGIN_ONLY)) {
            $this->api_output(ML_RCODE_NOLOGIN);
        }
        else if(!$this->check_permission(ML_PERMISSION_LOGIN_CANWRITE)) {
            $this->api_output(ML_RCODE_NOACTIVE);
        }
        if(empty($this->tags) || count($this->tagArr) == 0)
        $this->api_output(ML_RCODE_PARAM);        

        foreach($this->tagArr as $val){
            $val=Tool_keyword::filterSensitiveWord($val);
            if(Tool_string::str_width($val) > ML_DATARULE_USER_SKILLTAGS_MAXLEN ){
                $this->api_output(ML_RCODE_PARAM);
            }
            $val=Tool_keyword::filterSensitiveWord($val);
        }
    }

    public function main()
    {
        $this->tagArr=array_unique($this->tagArr);

        $rs=$this->obrief->updateSkillTags($this->uid,$this->tagArr);
        if(!rs)
        $this->api_output(ML_RCODE_BUSY , '' , ML_RMESSAGE_BUSY);


        if(array_intersect($this->oldTagArr,$this->tagArr) != $this->tagArr){
                
            $arr=array_diff($this->tagArr,$this->oldTagArr);
            $this->tags=implode(',',$arr);
            $oGenerator = new ml_model_dbIdGenerator();
            $userfeed_res=$oGenerator->get_userfeed_rid($this->uid,$rid);
            if(!$userfeed_res){
                $this->api_output(ML_RCODE_BUSY , '' , ML_RMESSAGE_BUSY);
            }else{
                $data=array(
                        'rid' => $rid,
                        'uid' => $this->uid,
                        'content' => $this->tags,
                        'type'=> ML_FEED_TYPE_MOD_TAG,
                        'ctime'=> date('Y-m-d H:i:s',time()),
                        'cip' => Tool_ip::get_real_ip()
                );
                $oUserfeed=new ml_model_dbUserfeed();
                if($oUserfeed->insert_content($data)){
                        
                    $data = array('feed_rid' => $rid, 'uid' => $this->uid);
                    ml_tool_sendMq::add_feed($data);
                    ml_tool_sendMq::search_user('A', $this->uid);
                    $this->api_output(ML_RCODE_SUCC, '');
                }else{
                    $this->api_output(ML_RCODE_BUSY , '' , ML_RMESSAGE_BUSY);
                }
            }
        }
        $this->api_output(ML_RCODE_SUCC);
    }
}
new aj_updateMyTag();